#!/usr/bin/python

import types

import rpm

rpmsense_map = {}
rpmtag_map = {}

logf = None
dolog = True
def openlog(filename):
    global logf, dolog
    if dolog:
        try:
            logf = file(filename, 'wa')
        except:
            print 'Open log file %s failed.\n' % filename
            logf = None

def dolog(str):
    global logf, dolog
    if dolog:
        if logf:
            logf.write(str)
            logf.flush()
        #print str,

def closelog():
    global logf,dolog
    if dolog:
        if logf:
            logf.close()

def get_rpmsense_map():
    hstr = 'RPMSENSE_'
    for item in rpm.__dict__:
        if item.startswith(hstr):
            rpmsense_map[item] = rpm.__dict__[item]

def get_rpmtag_map():
    hstr = 'RPMTAG_'
    for item in rpm.__dict__:
        if item.startswith(hstr):
            rpmtag_map[item] = rpm.__dict__[item]
get_rpmsense_map()
    
get_rpmtag_map()

def sense2arr(sense):
   result = []
   #for s in ['EQUAL', 'FIND_PROVIDES', 'FIND_REQUIRES', 'GREATER',
   #          'INTERP', 'KEYRING', 'LESS', 'MULTILIB', 'PATCHES', 'PREREQ',
   #          'RPMLIB', 'SCRIPT_BUILD', 'SCRIPT_CLEAN', 'SCRIPT_INSTALL',
   #          'SCRIPT_POST', 'SCRIPT_POSTUN', 'SCRIPT_PRE', 'SCRIPT_PREP',
   #          'SCRIPT_PREUN', 'SCRIPT_VERIFY', 'SERIAL', 'TRIGGERIN',
   #          'TRIGGERPOSTUN', 'TRIGGERPREIN', 'TRIGGERUN']:
   for s in rpmsense_map.keys():
      mask = rpmsense_map[s]
      if sense & mask:
         result.append(s)
         sense = sense & (~mask)
   #result.append(sense)
   return  result

def addHdr(hdr):
   #print "\t", hdr
   rpmname = hdr[rpm.__dict__['RPMTAG_NAME']]
   dolog("'%s':[" % rpmname)
   zip_r = None
   zip_p = None
   z_rver = None
   z_rflag = None
   z_rfile = None
   z_pver = None
   z_pflag = None
   z_pfile = None
   #for tag in ['GROUP',
   #            'REQUIRENAME', 'REQUIREVERSION', 'REQUIREFLAGS',
   #            # 'PROVIDES', PROVIDES is deprecated.
   #            'PROVIDENAME', 'PROVIDEVERSION', 'PROVIDEFLAGS',
   #            'FILENAMES']:
   for tag in rpmtag_map.keys():
      if tag == 'RPMTAG_REQUIREFLAGS' or tag == 'RPMTAG_PROVIDEFLAGS':
         result = []
         flaglist = hdr[rpm.__dict__[tag]]
         if type(flaglist) == types.ListType:
            for flags in flaglist:
               flagsarr = sense2arr(flags)
               result.append(flagsarr)
         else:
            result.append(sense2arr(flaglist))
         if tag == 'RPMTAG_REQUIREFLAGS':
            z_rflag = result
         elif tag == 'RPMTAG_PROVIDEFLAGS':
            z_pflag = result
#         print '\t%s:\t%s' % (tag, str(result))
      else:
         try:
            result = [hdr[rpm.__dict__[tag]]]
            if tag == 'RPMTAG_REQUIREVERSION':
               z_rver = result
            if tag == 'RPMTAG_PROVIDEVERSION':
               z_pver = result
            if tag == 'RPMTAG_REQUIRES':
               z_rfile = result
            if tag == 'RPMTAG_PROVIDES':
               z_pfile = result
#            print '\t%s:\t%s' % (tag, result)
         except:
            print 'ERROR'
            print tag
   #import pdb;pdb.set_trace()
   zip_r = zip(z_rfile[0], z_rver[0], z_rflag)
   zip_p = zip(z_pfile[0], z_pver[0], z_pflag)
   dolog("\n\t%s ," % str(zip_r))
   dolog("\n\t%s ," % str(zip_p))
   dolog('\n]\n,')

if __name__ == '__main__':
    import os 
    import glob
    logfile = 'rpm_rp.log'
    openlog(logfile)
    dolog('rpm_rp_map = {\n')
    ts = rpm.TransactionSet()
    pkgs_root = '../spec/packages'
    for rpmfnpath in glob.glob('%s/*.rpm' % pkgs_root):
        rpmfd = os.open(rpmfnpath, os.O_RDONLY)
        hdr = ts.hdrFromFdno(rpmfd)
        addHdr(hdr)
        os.close(rpmfd)
    dolog('\n}\n')
    closelog()

